package com.lzy.huijinjiazu.mvp.ui.activity;

import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.gyf.barlibrary.ImmersionBar;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.lzy.huijinjiazu.R;
import com.lzy.huijinjiazu.di.component.DaggerConfirmOrderComponent;
import com.lzy.huijinjiazu.di.module.ConfirmOrderModule;
import com.lzy.huijinjiazu.mvp.contract.ConfirmOrderContract;
import com.lzy.huijinjiazu.mvp.presenter.ConfirmOrderPresenter;
import com.lzy.huijinjiazu.mvp.ui.adapter.OrderChildAdapter;
import com.lzy.huijinjiazu.mvp.ui.dialog.PaymentDialog;
import com.lzy.huijinjiazu.mvp.ui.listener.ScrollViewListener;
import com.lzy.huijinjiazu.mvp.ui.widget.SlideScrollView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;


public class ConfirmOrderActivity extends BaseActivity<ConfirmOrderPresenter> implements ConfirmOrderContract.View, ScrollViewListener {


    @BindView(R.id.scrollView)
    SlideScrollView mScrollView;
    @BindView(R.id.fl_title)
    FrameLayout mFlTitle;
    @BindView(R.id.rv_list)
    RecyclerView mRvList;
    @BindView(R.id.tv_total)
    TextView mTvTotal;
    @BindView(R.id.tv_total_bottom)
    TextView mTvTotalBottom;
    @BindView(R.id.tv_commit)
    TextView mTvCommit;
    private OrderChildAdapter mAdapter;
    private PaymentDialog mPayDialog;

    @Override
    public void setupActivityComponent(AppComponent appComponent) {
        DaggerConfirmOrderComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .confirmOrderModule(new ConfirmOrderModule(this))
                .build()
                .inject(this);
    }

    @Override
    public int initView(Bundle savedInstanceState) {
        return R.layout.activity_confirm_order; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        mScrollView.setScrollViewListener(this);
        mTvCommit.setBackgroundColor(ContextCompat.getColor(this, R.color.colorAccent));
        mTvTotal.setText(Html.fromHtml(ArmsUtils.getString(this, R.string.order_total_money, "2", "1000", "200")));
        mTvTotalBottom.setText(Html.fromHtml(ArmsUtils.getString(this, R.string.order_total_money, "2", "1000", "200")));
        initRecyclerView();
        initTitle();
        initData();

    }

    private void initTitle() {
        mFlTitle.setBackgroundResource(R.mipmap.bg_theme_title);
        mFlTitle.getBackground().setAlpha(0);
    }

    private void initRecyclerView() {
        ArmsUtils.configRecyclerView(mRvList, new LinearLayoutManager(this) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        });
        mAdapter = new OrderChildAdapter();
        mRvList.setAdapter(mAdapter);
    }

    private void initData() {
        List<String> data = new ArrayList<>();
        for (int i = 0; i < 2; i++) {
            data.add("");
        }
        mAdapter.setNewData(data);
    }

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
        mImmersionBar.setTitleBar(this, mFlTitle);
    }

    @OnClick(R.id.tv_commit)
    public void onViewClicked() {
        if (mPayDialog == null) {
            mPayDialog = new PaymentDialog(this);
        }
        mPayDialog.show();
    }

    @Override
    public void onScrollChanged(SlideScrollView scrollView, int x, int y, int oldx, int oldy) {
        if (mFlTitle == null) {
            return;
        }
        if (y > 350) {
            mFlTitle.getBackground().setAlpha(255);
            return;
        }
        if (y <= 250 && y > 0) {
            mFlTitle.getBackground().setAlpha(y);
        }
    }
}
